package com.clothes.views;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.clothes.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/1 0001.
 *
 */

public class AddLinearLayout extends LinearLayout{

    private Context context;
    private LinearLayout llContainer;
    private String name;
    private List<String> list = new ArrayList<>();
    private List<String> nameList = new ArrayList<>();

    public AddLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        llContainer = this;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }

    //添加一个view
    public void addView(int num,List<String> mList){
        llContainer.removeAllViews();
        list.clear();
        if(mList != null && mList.size()>0)
            num = mList.size();
        for (int i=0;i<num;i++) {
            String txt = "";
            if(mList != null && mList.size()>0)
                txt = mList.get(i);
            View view = getView(txt);
            llContainer.addView(view);
            view.setPadding(6,4,6,4);
            LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
            params.setMargins(0,0,20,0);
            view.setLayoutParams(params);
        }
        invalidate();
    }

    private View getView(String txt){
        final View view = LayoutInflater.from(context).inflate(R.layout.item_edit,null);
        EditText etColor = view.findViewById(R.id.et_color);
        etColor.setHint(name);
        if(txt.equals("")){
            etColor.setEnabled(true);
        }else {
            etColor.setEnabled(false);
        }
        etColor.setText(txt);
        if(name.equals("数量"))
            etColor.setInputType(InputType.TYPE_CLASS_NUMBER);
        etColor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String txt = s.toString().trim();
                if(onTextChangeListener != null){
                    onTextChangeListener.txtChange(txt);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        return view;
    }

    public List<String> getList(){
        list.clear();
        for (int i=0;i<llContainer.getChildCount();i++){
            EditText et = (EditText) ((LinearLayout) llContainer.getChildAt(i)).getChildAt(0);
            list.add(et.getText().toString());
        }
        return list;
    }

    public String getName(int position){
        EditText et = (EditText) ((LinearLayout) llContainer.getChildAt(position)).getChildAt(0);
        return et.getText().toString();
    }

    //判断是否有相同的数据
    public boolean isCopy(){
        Map<String,Integer> map = new HashMap<>();
        for (String s:getList()){
            map.put(s,0);
        }
        if(map.size() < getList().size()){ //有重复的
            return true;
        }
        return false;
    }

    public boolean isEmpty(){
        boolean flag = false;
        for (String ss:getList()){
            if(ss.equals("")){
                flag = true;
                break;
            }
        }
        return flag;
    }

    public onTextChangeListener onTextChangeListener;
    public interface onTextChangeListener{
        void txtChange(String txt);
    }

    public void setOnTextChangeListener(onTextChangeListener onTextChangeListener) {
        this.onTextChangeListener = onTextChangeListener;
    }
}
